1633B - Minority - CodeForces Solution


greedy *800

Please click on ads to support us..

Python Code:

def solve():
    s=input()
    one=s.count("1")
    zero=s.count("0")
    if one==zero: print(one-1)
    else: print(min(one,zero))
t=int(input())
while t:
    solve()
    t-=1

C++ Code:

#include <bits/stdc++.h>
using namespace std;


int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    int n;
    cin >> n;
    string s;
    for (int i = 0; i < n; i++){
        string s;
        cin >> s;
        if(count(s.begin(), s.end(), '1')==count(s.begin(), s.end(), '0')){
            cout << s.size()/2 -1 << endl;
        }
        else if(count(s.begin(), s.end(), '1')>count(s.begin(), s.end(), '0')){
            cout << count(s.begin(), s.end(), '0') << endl;
        }
        else if(count(s.begin(), s.end(), '1')<count(s.begin(), s.end(), '0')){
            cout << count(s.begin(), s.end(), '1') << endl;
        }
    }
    return 0;
}
 


Comments

Submit
0 Comments
More Questions

822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion